package com.cheyun.netsalev3.view.orderlist

import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import com.bumptech.glide.Glide
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.ActivityOrderDetailsBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.cheyun.netsalev3.utils.InjectorUtils
import com.cheyun.netsalev3.viewmodel.orderlist.OrderDetailsActivityViewModel

class OrderDetailsActivity : BaseActivity<ActivityOrderDetailsBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_order_details
    }

//    private lateinit var orderAdapter: MyAdapter
    private lateinit var carAdapter: MyAdapter

    var oid = 0
    var tid = 0
    override fun initViewModel(): ViewModel? {
        if(intent.hasExtra("oid")) {
            oid = intent.getIntExtra("oid", 0)
        }
        if(intent.hasExtra("tid")) {
            tid = intent.getIntExtra("tid", 0)
        }

        var factory = InjectorUtils.getOrderDetailsActivityViewModelFactory(tid,oid)
        return ViewModelProviders.of(this, factory).get(OrderDetailsActivityViewModel::class.java)
    }

    override fun initData() {
        binding.viewModel?.pic?.observe(this, Observer {
            setImage(it)
        })

//        binding.viewModel?.orderList?.observe(this, Observer { list ->
//            orderAdapter.submitList(list)
//        })
        binding.viewModel?.carList?.observe(this, Observer { list ->
            carAdapter.submitList(list)
        })
    }

    override fun initView() {
//        orderAdapter = MyAdapter(R.layout.order_nodelist_item, binding.viewModel)
//        binding.swipeOrder.adapter = orderAdapter

        carAdapter = MyAdapter(R.layout.car_nodelist_item, binding.viewModel)
        binding.swipeCar.adapter = carAdapter
    }
    fun setImage(img:String){
        if(img.isNotEmpty()) {
            Glide.with(this).load(img).into(binding.ivCarImg)
        }else{
            binding.ivCarImg.setImageResource(R.drawable.lansemorenche)
        }
    }

}
